<?php
class HososController extends AppController {
	public $uses = array('Hoso','Tthc','ThanhPhan','ThanhPho','ThanhPhanTthc','Status','Attachment','ThanhPhanHoso','Ketquahanhchinh');
//	var $scaffold;
	
	public function beforeFilter() {
		$this->Auth->allow('*');
		$this->layout = 'tiepdan';
		$this->set("actionName", $this->request->params['action']);
	}
	
	public function index() {
		$this->redirect(array('action' => 'listHosoDangKyQuaMang'));
	}
	
	public function listHosoDangKyQuaMang () {
		
		$listHosos = $this->Hoso->find("all", array(
			'conditions' => array(
				'OR' => array(
					array(
						'Hoso.trang_thai' => $this->Status->HS_HEN_TIEP_NHAN_HS_GOC,
						'Hoso.ngay_hen_gap >=' => date("Y-m-d 00:00:00"),
						'Hoso.ngay_hen_gap <=' => date("Y-m-d 23:59:59")
					),
					array(
						'Hoso.trang_thai' => array($this->Status->HS_DANGKY_HOP_LE,$this->Status->HS_DANGKY_KHONG_HOP_LE)
					),
       			)
			), 
			'order' => array('Hoso.ngay_dang_ky' => 'desc')
		));
		
		// debug($listHosos);
		
		$this->set('listHosos', $listHosos);
	}
	
	public function listHosoDangXepHang () {
		$listHosos = $this->Hoso->find("all", array(
			'conditions' => array(
				'Hoso.trang_thai' => array(
					$this->Status->CHO_GOI, 
					$this->Status->KHONG_CO_MAT),
				'Hoso.ngay_hen_gap >=' => date("Y-m-d 00:00:00"),
				'Hoso.ngay_hen_gap <=' => date("Y-m-d 23:59:59")
			),
			'order' => array('Hoso.ngay_dang_ky' => 'asc')
		));
		
		// debug($listHosos);
		
		$this->set('listHosos', $listHosos);
	}
	
	public function listHosoDangXuLy () {
		
		$status = array(
			$this->Status->CHO_XULY,
			$this->Status->CHO_PHAN_XULY,
			$this->Status->HS_HOP_LE,
			$this->Status->HS_KHONG_DAT,
			$this->Status->BO_SUNG_HS,
			$this->Status->HS_DU_YEU_CAU ,
			$this->Status->HS_DA_XULY_XONG,
			$this->Status->PHAN_XULY_TU_DONG,
			$this->Status->PHAN_XULY_TRUC_TIEP,
			$this->Status->HS_DANG_XULY,
			$this->Status->LAY_HS_TON_DONG,
			$this->Status->HS_CHO_KIEM_DUYET,
			$this->Status->HS_CHO_KY,
			$this->Status->HS_KHONG_DUOC_DUYET,
			$this->Status->HS_DA_DUYET,
			$this->Status->HS_KHONG_DUOC_KY,
			$this->Status->HS_DA_KY,
		);
		
		$listHosos = $this->Hoso->find("all", array(
			'conditions' => array('Hoso.trang_thai' => $status),
			'order' => array('Hoso.ngay_dang_ky' => 'desc')
		));
		
		$this->set('listHosos', $listHosos);
	}
	
	public function listHosoQuaHan () {
	}
	
	public function listHosoDangChoKetQua () {
		$listHosos = $this->Hoso->find("all", array(
				'conditions' => array(
				'Hoso.trang_thai' => array($this->Status->HS_CHO_TRA_KQ, $this->Status->HS_KHONG_DAT_CHO_TRA_KQ),
			),
			'order' => array('Hoso.ngay_dang_ky' => 'desc')
		));
		
		
		$this->set('listHosos', $listHosos);
	}
	
	public function listHosoTreHen () {
		$listHosos = $this->Hoso->find("all", array(
				'conditions' => array(
				'Hoso.trang_thai' => array($this->Status->HS_TRE_HEN),
			),
			'order' => array('Hoso.ngay_dang_ky' => 'desc')
		));
		
		
		$this->set('listHosos', $listHosos);
	}
	public function listKetQuaHanhChinh () {
		$listKetquas = $this->Ketquahanhchinh->find("all",array(
			'order' => array('created' => 'desc')
		));
		
		$this->set('listKetquas', $listKetquas);
	}
	
	public function listHuongDan () {
		$this->set('path', 'http://tthc.ascom.vn/pages/');
	}
	
	public function dangkythanhcong () {
		$this->layout = 'dangky';
	}
	public function dangkyxephangthanhcong () {
		$this->layout = 'dangky';
	}
	public function dangkyonline ($tthcId = null) {
		$this->layout = 'dangky';
		// debug($this->request->data);
		
		$tthc = empty($tthcId)? $defaultTthc = $this->Tthc->findById(1): $this->Tthc->findById($tthcId);
		// debug($tthc);
		$this->__initForm($tthc, $this);
		
		if ($this->request->isPost()) {
			// debug($this->request->data);
			
			$this->request->data['Hoso']['trang_thai'] = $this->Status->CHO_DUYET_QUA_MANG;
			$this->request->data['Hoso']['loai'] = 1; // 1: Dang ky online 2: Dang ky truc tiep
			$this->request->data['Hoso']['ten'] = $this->request->data['Hoso']['hovaten'];
			$this->request->data['Hoso']['ngay_dang_ky'] = date("Y-m-d H:i:s");
			if(isset($this->request->data['Hoso']['ngaycap'])) $this->request->data['Hoso']['ngay_cap'] = $this->convertStrDate($this->request->data['Hoso']['ngaycap']);
			$this->request->data['Hoso']['code'] = $this->__generateDocCode($tthc);
			
			//debug($this->request->data);
			if($this->Hoso->save($this->request->data)) {
				$hoso = $this->request->data['Hoso'];
				$thanhPhans = $this->ThanhPhan->getListThanhPhanGiayToByTthcId($hoso['tthc_id']);
				//debug($hoso);
				foreach ($thanhPhans as $key => $thanhPhan) {
					$uploadFile = $hoso['file_'.$thanhPhan['ThanhPhan']['id']];
					
					$newFileName = $this->__generateUniqueFilename($uploadFile['name'],'');
					move_uploaded_file($uploadFile['tmp_name'], WWW_ROOT.'/files/'.$newFileName);
					
					$attachment = array();
					$attachment['Attachment'] = array();
					
					$attachment['Attachment']['original_name'] = $uploadFile['name'];
					$attachment['Attachment']['code'] = $newFileName;
					$attachment['Attachment']['hoso_id'] = $this->Hoso->getLastInsertId();
					$attachment['Attachment']['thanh_phan_id'] = $thanhPhan['ThanhPhan']['id'];
					
					$this->Attachment->create();
					$this->Attachment->save($attachment);
					
					
				}
				
				// saving attachements
				/*foreach ($this->request->data['Hoso']['files'] as $key => $file) {
					$newFileName = $this->__generateUniqueFilename($file['name'],'');
					move_uploaded_file($file['tmp_name'], WWW_ROOT.'/files/'.$newFileName);
					
					$attachment = array();
					$attachment['Attachment'] = array();
					$attachment['Attachment']['original_name'] = $file['name'];
					$attachment['Attachment']['code'] = $newFileName;
					$attachment['Attachment']['hoso_id'] = $this->Hoso->getLastInsertId();
					
					$this->Attachment->create();
					$this->Attachment->save($attachment);
				}*/
				
				$this->redirect(array('action'=>'dangkythanhcong'));
			}
		}

		// debug($tthcId);
		// set data to view
		$this->set('tthcId', $tthcId);
		$this->set("errors", $this->Hoso->validationErrors);
	}
	public function dangky($tthcId = 0) {
		$this->layout = 'dangky';
		
		$tthc = empty($tthcId)? $defaultTthc = $this->Tthc->findById(1): $this->Tthc->findById($tthcId);
		
		$this->__initForm($tthc, $this);
		
		if ($this->request->isPost()) {
			// debug($this->request->data);
			
			$this->request->data['Hoso']['trang_thai'] = $this->Status->CHO_GOI;
			$this->request->data['Hoso']['loai'] = 2; // 1: Dang ky online 2: Dang ky truc tiep
			$this->request->data['Hoso']['ten'] = $this->request->data['Hoso']['hovaten'];
			$this->request->data['Hoso']['ngay_dang_ky'] = date("Y-m-d H:i:s");
			$this->request->data['Hoso']['ngay_hen_gap'] = date("Y-m-d H:i:s");
			$this->request->data['Hoso']['ngay_cap'] = $this->convertStrDate($this->request->data['Hoso']['ngaycap']);
			$this->request->data['Hoso']['code'] = $this->__generateDocCode($tthc);
			
			//debug($this->request->data);
			if($this->Hoso->save($this->request->data)) {
				$this->redirect(array('action'=>'dangkyxephangthanhcong'));
			}
		}

		$this->set("errors", $this->Hoso->validationErrors);
	}
	private function __generateDocCode ($tthc) {
		// debug($tthc);
		$date = date('Ymd');
		return $tthc['Tthc']['code'].$date; 
	}
	
	private function __initForm($tthc, $obj) {
		$defaultTthc = $tthc;
		
		$thanhPhos = $this->ThanhPho->find("list", array('fields'=>array('code','ten')));
		$tthcs = $this->Tthc->find("list", array('fields'=>array('id','ten'),'conditions' => array('activeFlg' => 1)));
		//debug($tthcs);
		// empty($tthc) && ($defaultTthc = $this->Tthc->findById(1));
		
		//$defaultTthc = $this->Tthc->findById(1);
		
		$thanhPhans = $this->ThanhPhan->getListThanhPhanGiayToByTthcId($defaultTthc['Tthc']['id']);
		
		// debug($tthcs);
		// debug($defaultTthc);
				
		$obj->set(compact('tthcs'));
		$obj->set(compact('thanhPhos'));
		$obj->set("selectedTthc", $defaultTthc);
		$obj->set("thanhphans", $thanhPhans);
	}
	
	public function dangkytructiep () {
		$this->render("dang_ky_truc_tiep");
	}
	
	public function cancel () {
		$this->redirect('index' );
	}

	private function __generateUniqueFilename($fileName, $path='') {
		$path = empty($path) ? WWW_ROOT.'/files/' : $path;
		$no = 1;
		$newFileName = $fileName;
		while (file_exists("$path/".$newFileName)) {
			$no++;
			$newFileName = substr_replace($fileName, "_$no.", strrpos
			($fileName, "."), 1);
		}
		return $newFileName;
	}
	
	public function quytrinh () {
	}
}
	